/*
===========================================================================
This source file is part of DyLab (Dynamic Laboratory)
For the latest info, see http://dylab-modules.googlecode.com

Copyright (c) 2006-2008 Lukas Krejci
(krejci.lukas@volny.cz)

This file is part of DyLab.

    DyLab is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    DyLab is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DyLab.  If not, see <http://www.gnu.org/licenses/>.
===========================================================================
*/

//===========================================================================================
#include "Ogre_Gfx.h"

//===========================================================================================
dylab::Ogre_Gfx::GuiSystem::GuiSystem(const String & _name, Window * _window, Scene * _scene)
: mName(_name), mWindow(_window), mScene(_scene), mMousePos(Vector2::ZERO), mMousePanel(NULL)
{
    // check the arguments
    DYLAB_ASSERT(mWindow != NULL);
    DYLAB_ASSERT(mScene != NULL);       

    mMouseMaterial = Ogre::MaterialManager::getSingleton().create("MouseCursor/default", "General");

    Ogre::Overlay * mouseOverlay = Ogre::OverlayManager::getSingletonPtr()->create("MouseOverlay");
	mouseOverlay->setZOrder(600);

    mMousePanel = (Ogre::PanelOverlayElement *)Ogre::OverlayManager::getSingletonPtr()->createOverlayElement("Panel", "GUIMouse");
    mMousePanel->setMaterialName(mMouseMaterial->getName());
	mMousePanel->setPosition(0, 0);
	mMousePanel->setWidth(0);
	mMousePanel->setHeight(0);

    Ogre::OverlayContainer * mouseContainer 
        = (Ogre::OverlayContainer*) Ogre::OverlayManager::getSingletonPtr()->createOverlayElement("Panel", "GUIContainer");
	mouseOverlay->add2D(mouseContainer);

	mouseContainer->addChild(mMousePanel);
	mouseOverlay->show();
}
//-----------------------------------------------------------------------------
dylab::Ogre_Gfx::GuiSystem::~GuiSystem()
{
}
//-----------------------------------------------------------------------------
dylab::String dylab::Ogre_Gfx::GuiSystem::getName() const
{
    return mName;
}
//-----------------------------------------------------------------------------
void dylab::Ogre_Gfx::GuiSystem::setMousePosition(const Vector2  & _relPos)
{
    mMousePos += _relPos;
    mMousePos.clamp(Vector2::ZERO, Vector2::ONE);
    mMousePanel->setPosition(mMousePos.x(), mMousePos.y());	    
}
//-----------------------------------------------------------------------------
void dylab::Ogre_Gfx::GuiSystem::setMouseImage(const String & _fileName)
{
    mMouseTexture = Ogre::TextureManager::getSingleton().load(_fileName, "General");

    Ogre::TextureUnitState *pTexState;
	if(mMouseMaterial->getTechnique(0)->getPass(0)->getNumTextureUnitStates())	
		pTexState = mMouseMaterial->getTechnique(0)->getPass(0)->getTextureUnitState(0);	
	else	
		pTexState = mMouseMaterial->getTechnique(0)->getPass(0)->createTextureUnitState(mMouseTexture->getName());
	
	pTexState->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP);
    mMouseMaterial->getTechnique(0)->getPass(0)->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
	mMousePanel->setMaterialName(mMouseMaterial->getName());

    real_t winHeight = mWindow->getOgreRenderWindow()->getHeight();
	real_t winWidth  = mWindow->getOgreRenderWindow()->getWidth();

	mMousePanel->setWidth(mMouseTexture->getWidth() / winWidth);
	mMousePanel->setHeight(mMouseTexture->getHeight() / winHeight);
}
//-----------------------------------------------------------------------------
void dylab::Ogre_Gfx::GuiSystem::showMouse(bool _visible)
{
    if (_visible)
        mMousePanel->show();
    else
        mMousePanel->hide();
}
//-----------------------------------------------------------------------------
bool dylab::Ogre_Gfx::GuiSystem::isMouseVisible() const
{
    return mMousePanel->isVisible();
}
//-----------------------------------------------------------------------------
void dylab::Ogre_Gfx::GuiSystem::showGui(const String & _name, bool _show)
{
    // retrieve the gui specified by its name
    Ogre::Overlay * gui = Ogre::OverlayManager::getSingleton().getByName(_name);
    DYLAB_ASSERT_MSGF(gui != NULL, "Gui '%1%' not found.", _name);

    // show/hide the gui
    if (_show)
        gui->show();
    else
        gui->hide();
}
//-----------------------------------------------------------------------------
bool dylab::Ogre_Gfx::GuiSystem::isGuiVisible(const String & _name) const
{
    // retrieve the gui specified by its name
    Ogre::Overlay * gui = Ogre::OverlayManager::getSingleton().getByName(_name);
    DYLAB_ASSERT_MSGF(gui != NULL, "Gui '%1%' not found.", _name);

    return gui->isVisible();
}
//-----------------------------------------------------------------------------
void dylab::Ogre_Gfx::GuiSystem::setGuiElementCaption(const String & _elemName, const String & _caption)
{
    try 
    {
        Ogre::OverlayElement * elem = Ogre::OverlayManager::getSingleton().getOverlayElement(_elemName);
        elem->setCaption(_caption);
    }
    catch(...)
    {
        // ignore?
    }
}
//-----------------------------------------------------------------------------